package leetcode.code1546;

import java.util.HashSet;
import java.util.Set;

public class Solution {
	public int maxNonOverlapping(int[] nums, int target) {
		Set<Integer> set = new HashSet<>();
		set.add(0);
		int ans = 0;
		for (int i = 0, sum = 0; i < nums.length; i++) {
			sum += nums[i];
			if (set.contains(sum - target)) {
				ans += 1;
				set = new HashSet<>();
				set.add(0);
				sum = 0;
			} else {
				set.add(sum);
			}
		}
		return ans;
	}
}
